package com.zang.tree;

/**
 * 把二叉搜索树转换为累加树
 * @author ZVerify
 * @since 2022/11/03 09:49
 * @see <a href="https://leetcode.cn/problems/convert-bst-to-greater-tree/description/">...</a>
 **/
public class 把二叉搜索树转换为累加树 {
    int sum = 0;
    public TreeNode convertBST(TreeNode root) {

        dfs(root);
        return root;
    }

    private void dfs(TreeNode root) {
        if (root == null) return;
        dfs(root.right);
        root.val = root.val+sum;
        sum = root.val;
        dfs(root.left);
    }
}
